package day01;

import java.util.Stack;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/12 12:22
 * 力扣 20. 有效的括号
 */
public class StackPractice01 {
    public boolean isValid(String s) {
        if(s.length() < 2) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '{' || c == '[' || c == '(') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) return false;
                char p = stack.pop();
                if (c == ')' && p != '(') return false;
                if (c == '}' && p != '{') return false;
                if (c == ']' && p != '[') return false;
            }
        }
        return stack.isEmpty();
    }
}
